package base.http.rxandroid

import io.reactivex.disposables.Disposable
import java.lang.ref.WeakReference

/**
 * Created by Jack on 2017/10/11.
 */

class CompositeDisposableUtilsV2 private constructor() {

    private val subscriptions = HashSet<WeakReference<CustomDisposableSubscriber<*>>>()

    companion object {
        val disposableUtils = getInstance.instance
    }

    object getInstance {
        val instance = CompositeDisposableUtilsV2()
    }

    fun add(disposable: Disposable) {
//        disposableUtils.add(disposable)
    }

    private fun clear() {
        subscriptions.clear()
    }


    fun addSubscription(subscription: CustomDisposableSubscriber<*>) {
        val weakSubscription = WeakReference<CustomDisposableSubscriber<*>>(subscription)
        subscriptions.add(weakSubscription)
    }

    fun unSubscriptions() {
        for (s in subscriptions) {
            try {
                val customDialog = s.get()?.dialogConsumer
                customDialog?.dialog?.dismissDialog()
                s.get()?.dispose()
            } catch (e: Throwable) {
                e.printStackTrace()
            }
        }
    }

//    fun isSubscriptions(): Boolean {
//        for (s in subscriptions) {
//            if (!s.isDisposed()) {
//                return false
//            }
//        }
//        return true
//    }

}
